package com.roomio.domain;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.springframework.format.annotation.DateTimeFormat;

import com.roomio.core.model.BaseObject;
import com.roomio.core.security.model.User;

@NamedQueries(value={
		@NamedQuery(name="group.by.name.and.username",query=" select ug  from Group ug join ug.users user where name=:name and user.id.user.email=:userName"),
		@NamedQuery(name="group.by.username",query="  from Group ug  where groupOwner.email=:userName")
})

@Entity
@Table (name="GROUPS")
public class Group extends BaseObject {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	private Long id;

	private String name;
	
	@ManyToOne(fetch = FetchType.EAGER, cascade= { CascadeType.REFRESH })
	@JoinColumn(name = "GROUP_OWNER", nullable=false)
	private User groupOwner;

	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "M-")
	@Column(name = "CREATED_DATE",nullable=false )
	private Date createdDate= new Date();
	
	@OneToMany(fetch = FetchType.EAGER,  mappedBy = "group", cascade = {CascadeType.ALL })
	protected Set<UserGroup> users = new HashSet<UserGroup>();

	public Set<UserGroup> getUsers() {
		return users;
	}

	public void setUsers(Set<UserGroup> users) {
		this.users = users;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}	
	
	public User getGroupOwner() {
		return groupOwner;
	}

	public void setGroupOwner(User groupOwner) {
		this.groupOwner = groupOwner;
	}

	public void addUser(User user){
		UserGroup userGroup = new UserGroup();
		userGroup.setUser(user);
		userGroup.setGroup(this);
		users.add(userGroup);		
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Group other = (Group) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	
}
